﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace EventCheck {
    public class Program {

        static void Main(string[] args) {

            try {
                if (args.Length > 0) {                  
                    string path = AppDomain.CurrentDomain.BaseDirectory + "config.txt";
                    FileStream file = new FileStream(path, FileMode.Open);
                    StreamReader sr = new StreamReader(file);
                    string strLine = sr.ReadToEnd();

                    List<ConfigModel> config = new List<ConfigModel>();
                    config = JsonstringtoList(strLine);
                    SQLString = "Data Source=" + config[0].Host + " ;Initial Catalog=" + config[0].DBName + ";User ID=" + config[0].UserName + ";Password=" + config[0].PassWord + "";

                    unitId = config[0].UNITY_ID;
                    eventCheckEnabled = config[0].EventCheckEnabled;


                    // 关闭文件
                    sr.Close();
                    file.Close();
                    Console.WriteLine("条码:{0}", args[0]);
                    CreateFile("OK,return 0");



                    Environment.Exit(checkEvent(args[0]) ? 0 : -1);
                   
                 
                } else {
                   Console.WriteLine("条码:{0}", args[0]);
                    CreateFile("NO,return -1");

                    Environment.Exit(-1);
                }
            
            } catch (Exception e) {
                CreateFile("error,return -1");
                Environment.Exit(-1);
            }
           
        }

        private static bool eventCheckEnabled ;
        private static int unitId;

        private static string SQLString;
        public static bool checkEvent(string barcode) {

            DbMapper.service.SQLHelper sqlHelper = new DbMapper.service.SQLHelper();
            if (!eventCheckEnabled) {

                return true;

            } else {
                string sql = "SELECT*from RESULTS_HEADER where BARCODE=" + "'" + barcode + "'" + " AND UNIT_ID=" + unitId + " ORDER BY SEQNO DESC";

                var dt = sqlHelper.getBarCodeFromTable(sql, SQLString);

                if (dt.Rows.Count != 0) {
                    if ((dt.Rows[0]["FailCode"].ToString() == "30001"|| dt.Rows[0]["FailCode"].ToString() == "100") &&dt.Rows[0]["REWORK"] is DBNull) {

                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }



            }


        }


        public static List<ConfigModel> JsonstringtoList(string jsonstr) {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            var objs = Serializer.Deserialize<List<ConfigModel>>(jsonstr);
            return objs;
        }
        public static ConfigModel Jsonstring(string jsonstr) {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            var objs = Serializer.Deserialize<ConfigModel>(jsonstr);
            return objs;
        }

        public static void CreateFile(string jsonstring) {
            string path = AppDomain.CurrentDomain.BaseDirectory + "reocrd.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符，也可以指定任意文件名，还可以为word等文件
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); // 创建写入流
            fs.SetLength(0);
            sw.WriteLine(jsonstring); // 写入Hello World
            sw.Close(); //关闭文件
        }
    }
}
